<?php
/*
 * inc-question.php
 * Retral/CSCD494 Winter 2009
 * Eastern Washington University
 *
 * File Description:
 * Gives detailed information on a question and provides links for edits to be made
 * 
 * Other:
 * 
 */
?>

<link rel='stylesheet'
	href="css/preview.css"
	type="text/css" media="screen">

<?php
/*
 * Gives detailed information on a question and allows edits to be made.
 */

	require("KinID.php");

	$conn = oci_connect(ORA_CON_UN, ORA_CON_PW, ORA_CON_DB);

	$query = '
		SELECT question_type.type_description, question_content.points, 
		     question_content.question_title, question.date_modified, 
		     ape_user.first_name, ape_user.last_name
		FROM question_content INNER JOIN question 
		ON question.question_content_id=question_content.question_content_id
		INNER JOIN question_type 
		ON question.question_type_code=question_type.type_code
		INNER JOIN ape_user ON question_content.author_id=ape_user.user_id
		WHERE question_id=:QID AND q_version = :vNum';

	$id = $_REQUEST['id'];
	$v = $_REQUEST['ver'];
    
	$stmt = oci_parse($conn, $query);
	oci_bind_by_name($stmt, ':QID', $id);
	oci_bind_by_name($stmt, ':vNum', $v);
	if(!oci_execute($stmt))
		print "<P>Could not query database for Question $id";

	$basicInfo = oci_fetch_assoc($stmt);
	if(!$basicInfo)
		print "<P>Could not find Question $id</P>";

	oci_free_statement($stmt);
	
	$query = '
		SELECT AVG(grade) AS "AVG"
		FROM student_question WHERE question_id=:QID AND q_version=:vNum';

	$stmt = oci_parse($conn, $query);
	oci_bind_by_name($stmt, ':QID', $id);
	oci_bind_by_name($stmt, ':vNum', $v);

	if(!oci_execute($stmt))
		print "<P>Students are unknown</P>";

	$row = oci_fetch_assoc($stmt);
	$avg = $row["AVG"];

	oci_free_statement($stmt);
			
			
	// Set $minVal = 5 if not set, otherwise use current val
	if (!isset($_REQUEST['min']))
		$minVal = 5;
	else
		$minVal = $_REQUEST['min'];
		
	print "<span class='header-text'>View/Edit</span><br/>\n";
	echo "<h3><strong> Title: ".$basicInfo['QUESTION_TITLE']. "</strong> <a href='?page=editType&id=" . $id . "&ver=" . $v ."&min=$minVal'> &nbsp; edit </a></h3>";
	echo "Author: ".$basicInfo['FIRST_NAME']." ".
	      $basicInfo['LAST_NAME']."<br>\n";
	echo "Created: ".dateFormat($basicInfo['DATE_MODIFIED']);
	echo "\n<BR>Revision: $v";
	echo "\n<BR>Type: " . $basicInfo['TYPE_DESCRIPTION']; // . "<a href='?page=editType&id=" . $id . "&ver=" . $v ."&min=$minVal'> &nbsp; edit </a>";
	echo "\n<BR>Points: " . $basicInfo['POINTS']; // . "<a href='?page=editType&id=" . $id . "&ver=" . $v ."&min=$minVal'> &nbsp; edit </a>";
    ///echo "<br>taken $total times ";
	//echo round($avg,2)." average.";
	
	$sid = 0;
    $isfile = true;
	$temp = getInstructions($conn, $sid, $isfile);
    
    echo '<br><br>';
    if($isfile)
        echo "\n<h3>Instructions: <a href='?page=viewSource&id=" . $sid . "&qid=" . $id. "&ver=" . $v . "&min=$minVal'> &nbsp; edit </a></h3>";
	else
        echo "\n<h3>Instructions: <a href='?page=addSource&qid=" . $id. "&ver=" . $v . "&kin=" . instr() .  "&min=$minVal'> &nbsp; add instructions </a></h3>";
        
    if($temp != null)
        echo "\n<P>". $temp ."</p>";
    else
        echo "<P>No Instruction Result</P>";
	
	$sid = 0;
    $isfile = true;
	$temp = getCriteria($conn, $sid, $isfile);
    
    echo '<br><br>';
    if($isfile)
        echo "\n<h3>Scoring Criteria: <a href='?page=viewSource&id=" . $sid . "&qid=" . $id. "&ver=" . $v . "&min=$minVal'> &nbsp; edit </a></h3>";
	else
        echo "\n<h3>Scoring Criteria: <a href='?page=addSource&qid=" . $id. "&ver=" . $v . "&kin=" . scoring() .  "&min=$minVal'> &nbsp; add scoring criteria </a></h3>";
    
    if($temp != null)
        echo "\n<P>". $temp ."</p>";
    else
        echo "<P>No criteria Result</P>";

	$query = '
		SELECT source_file_id, source_file_name FROM source_file 
		WHERE question_id=:QID AND q_version=:vNum 
		     AND kin != :KIN1 AND kin != :KIN2';

	$stmt = oci_parse($conn, $query);
	oci_bind_by_name($stmt, ':QID', $id);
	oci_bind_by_name($stmt, ':vNum', $v);
	oci_bind_by_name($stmt, ':KIN1', scoring());
	oci_bind_by_name($stmt, ':KIN2', instr());

	if(!oci_execute($stmt))
		print "<p><b>Error: (Question) - SQL Statement failed to execute.</b></p>";

	echo '<br><br>';
    echo "\n<h3>Source Files : <a href='?page=addSource&qid=" . $id . "&ver=" . $v . "&min=$minVal'> &nbsp; add source file </a></h3>";
	$rows = 0;
    while($row = oci_fetch_assoc($stmt)) 
    {
		echo "<a href='?page=viewSource&id=" . $row['SOURCE_FILE_ID'] . "&qid=" . $id. "&ver=" . $v . "&name=" . $row['SOURCE_FILE_NAME'] . "&min=$minVal'>";
		echo $row['SOURCE_FILE_NAME']."</a><BR>\n";
        $rows++;
	}
    if($rows == 0)
        echo "No source files found for this question.";

	oci_free_statement($stmt);

	displayCRs($conn);
	
	oci_close($conn);

function getInstructions($conn, &$instrID, &$isfile) 
{
	$query = '
		SELECT source_file, source_file_id FROM source_file
		WHERE kin = :KIN AND question_id = :QID AND q_version = :vNum';

	$id = $_REQUEST['id'];
	$v = $_REQUEST['ver'];

	$stmt = oci_parse($conn, $query);
	oci_bind_by_name($stmt, ':QID', $id);
	oci_bind_by_name($stmt, ':vNum', $v);
	oci_bind_by_name($stmt, ':KIN', instr());
	if(!oci_execute($stmt))
		print "<P>instruction query failed</P>";

	$result = oci_fetch_assoc($stmt);
	if(!$result)
    {
        $isfile = false;
        return null;
    }
	$instrID = $result["SOURCE_FILE_ID"];
	return $result["SOURCE_FILE"]->load();
}

function getCriteria($conn, &$instrID, &$isfile) 
{
	$query = '
		SELECT source_file, source_file_id FROM source_file
		WHERE kin = :KIN AND question_id = :QID AND q_version = :vNum';

	$id = $_REQUEST['id'];
	$v = $_REQUEST['ver'];

	$stmt = oci_parse($conn, $query);
	oci_bind_by_name($stmt, ':QID', $id);
	oci_bind_by_name($stmt, ':vNum', $v);
	oci_bind_by_name($stmt, ':KIN', scoring());
	if(!oci_execute($stmt))
		print "<P>criteria query failed</P>";

    $result = oci_fetch_assoc($stmt);
	if(!$result)
    {
        $isfile = false;
        return null;
    }	
	$instrID = $result["SOURCE_FILE_ID"];
	return $result["SOURCE_FILE"]->load();

}

/* Add revision options for admin users only */
function displayCRs($conn) {
	if (isset($_SESSION['loggedIn']) && $_SESSION['roleCode'] == "admin")
	{
		$id = $_REQUEST['id'];

		// This is the value in the database that the database uses
		// to signify an admin response revision. Meaning of the 
		// numbers are not documented here because it will not
		// get updated.
		$responseValue = 2;
		$requestValue = 1;

		$query = '
			SELECT COUNT(question_id) AS "COUNT" FROM change_request
			INNER JOIN revision_type 
			ON change_request.request_code=revision_type.request_code
			WHERE question_id=:QID AND revision_type.rev_type = :RQ';
		$stmt = oci_parse($conn, $query);
		oci_bind_by_name($stmt, ':QID', $id);
		oci_bind_by_name($stmt, ':RQ', $requestValue);
		if(!oci_execute($stmt))
			print "<p>Bad query for CR count</p>";

		$result = oci_fetch_assoc($stmt);

		if($result['COUNT'] > 0)
		{
			print "<h3>Change Requests ".$result['COUNT']."</h3>";
			
			$query = '
				SELECT comments, q_version, question.date_modified,
				     revision_type.type_description, change_request.request_code 
				FROM change_request INNER JOIN revision_type 
				ON change_request.request_code = revision_type.request_code
				INNER JOIN question 
				ON question.question_id=change_request.question_id
				WHERE revision_type.rev_type = :RQ
				AND change_request.question_id=:QID
				ORDER BY question.date_modified DESC'; 

			$stmt = oci_parse($conn, $query);
			oci_bind_by_name($stmt, ':QID', $id);
			oci_bind_by_name($stmt, ':RQ', $requestValue);
			if(!oci_execute($stmt))
				print "<p>Bad query for CR information</p>";

			$query = '
				SELECT request_code, type_description 
				FROM revision_type WHERE rev_type >= :RSP
				ORDER BY type_description';

			$stmtOption = oci_parse($conn,$query);
			oci_bind_by_name($stmtOption, ':RSP', $responseValue);
			if (!oci_execute($stmtOption))
				echo "<p><b>Error: (Question - Revision dropdown)</b> SQL statement failed to execute</p>";

			$options = "";
			//Walk through DB listing each option that was requested
			while ($option = oci_fetch_assoc($stmtOption))
			{
				$options .= "\t<option value='".$option['REQUEST_CODE']."'>";
				$options .= $option['TYPE_DESCRIPTION']."</option>\n";
			}
			oci_free_statement($stmtOption);

			while($row = oci_fetch_assoc($stmt))
			{

				print dateFormat($row['DATE_MODIFIED'])."<br>\n";
				print $row['TYPE_DESCRIPTION']."<br>\n";
				print "Comments:<p>".$row['COMMENTS']."</p>\n";

				print "<form action='index.php' method='POST'>\n";
				print "<input type='hidden' name='page' value='request'>\n";
				print "<input type='hidden' name='qid' value='$id'>\n";
				print "<input type='hidden' name='qver' value='".$row['Q_VERSION']."'>\n";
				print "<input type='hidden' name='cr' value='".$row['REQUEST_CODE']."'>\n";
					
				echo "<b>Revision Decision:</b> \n";
				
				echo "<select name='rev' id='rev'>";
			
				echo $options;
					
				echo "</select>";
				echo "<input type='submit' value='Mark Question'>";
				echo "</form><hr>";
			}

			oci_free_statement($stmt);
		}
	}
}
?>

<script type="text/javascript"
	src="script/preview.js" language="javascript"></script>
